<template>
  <div>
    <div style="margin-top: -90px;margin-bottom: -20px">添加材料</div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
      <div style="margin-right: 125px;margin-left: 55px">
        <el-form-item label="材料标题" prop="title" >
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="材料信息" prop="message">
          <el-input type="textarea" v-model="ruleForm.message"></el-input>
        </el-form-item>
      </div>
      <el-form-item style="margin-left: -90px">
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {addMaterial} from "@/api/material";

export default {
  data() {
    return {
      ruleForm: {
        id: '',
        title: '',
        message: '',
      },

      rules: {
        title: [{ required: true, message: '请输入材料标题', trigger: 'blur' }],
        message: [{ required: true, message: '请输入材料信息', trigger: 'blur' }],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addMaterial(this.ruleForm).then(res=>{
            if(res.code==200){
              this.$message({type:'success',message:res.msg})
              this.$emit("closeAddDialog")
            }else{
              this.$message({type:'error',message:res.msg})
            }
          }).catch(error=>{})
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>
